﻿using System;
using System.Web.Mvc;

namespace Caramel.Web.Mvc
{
	public class ReadOnlyUnitOfWorkAttribute : ActionFilterAttribute 
	{
		private IUnitOfWork _work;

		public override void OnActionExecuting(ActionExecutingContext filterContext)
		{
			_work = UnitOfWork.BeginReadOnly();

			base.OnActionExecuting(filterContext);
		}

		public override void OnResultExecuted(ResultExecutedContext filterContext)
		{
			base.OnResultExecuted(filterContext);

			UnitOfWork.DisposeOfUnitOfWork(_work);
		}
	}
}
